/*
 * TilknytVagt.java
 *
 */
package hbk;

import java.sql.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Jakob Pless
 */
public class TilknytVagt extends javax.swing.JPanel {

    private DbConnector dbcon;
    private ResultSet result;

    /** Creates new form TilknytVagt */
    public TilknytVagt() {
        initComponents();

        // Der oprettes forbindelse til databasen
        dbcon = new DbConnector();

        // De statiske data bliver sat ind
        populateComboBoxes();
    }

    public void populateComboBoxes() {
        // Medarbejder Typer
        // De tre medarbejder typer bliver sat ind i combo boxen
        medarbTypeBox.addItem(new String("Vagt Personale"));
        medarbTypeBox.addItem(new String("Kiosk Personale"));
        medarbTypeBox.addItem(new String("Oprydning Personale"));

        // Dato
        // Talene 1-31 bliver sat ind i combo boxen via en for-løkke
        for (int i = 1; i < 32; i++) {
            dayBox.addItem(new String(i + ""));
        }

        // Månednavnene bliver sat ind i combo boxen
        monthBox.addItem(new String("Januar"));
        monthBox.addItem(new String("Februar"));
        monthBox.addItem(new String("Marts"));
        monthBox.addItem(new String("April"));
        monthBox.addItem(new String("Maj"));
        monthBox.addItem(new String("Juni"));
        monthBox.addItem(new String("Juli"));
        monthBox.addItem(new String("August"));
        monthBox.addItem(new String("September"));
        monthBox.addItem(new String("Oktober"));
        monthBox.addItem(new String("November"));
        monthBox.addItem(new String("December"));

        // Talene 1900-2009 bliver sat ind i combo boxen via en for-løkke
        for (int i = 1900; i < 2010; i++) {
            yearBox.addItem(new String(i + ""));
        }
    }

    public String getDate() {
        String date = "";
        int day = dayBox.getSelectedIndex() + 1;
        int month = monthBox.getSelectedIndex() + 1;
        int year = yearBox.getSelectedIndex() + 1900;

        date += year;

        if (month < 10) {
            date += "-0" + month;
        } else {
            date += "-" + month;
        }
        
        if (day < 10) {
            date += "-0" + day;
        } else {
            date += "-" + day;
        }

        return date;
    }

    public void getOmraade() {
        String date = getDate();
        result = dbcon.executeQuery("" + date);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        medarbTypeLbl = new javax.swing.JLabel();
        medarbTypeBox = new javax.swing.JComboBox();
        medarbLbl = new javax.swing.JLabel();
        medarbBox = new javax.swing.JComboBox();
        datoLbl = new javax.swing.JLabel();
        monthBox = new javax.swing.JComboBox();
        yearBox = new javax.swing.JComboBox();
        dayBox = new javax.swing.JComboBox();
        omraadeLbl = new javax.swing.JLabel();
        omraadeBox = new javax.swing.JComboBox();
        tilknyt = new javax.swing.JButton();

        medarbTypeLbl.setText("Medarbejder Type:");

        medarbTypeBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                medarbTypeBoxActionPerformed(evt);
            }
        });

        medarbLbl.setText("Medarbejder:");

        datoLbl.setText("Dato:");

        monthBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Januar", "Februar", "Marts", "April", "Maj", "Juni", "Juli", "August", "September", "Oktober", "November", "December" }));

        dayBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dayBoxActionPerformed(evt);
            }
        });

        omraadeLbl.setText("Område:");

        tilknyt.setText("Tilknyt");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(medarbTypeLbl)
                            .addComponent(medarbLbl)
                            .addComponent(datoLbl)
                            .addComponent(omraadeLbl))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(medarbBox, 0, 196, Short.MAX_VALUE)
                            .addComponent(medarbTypeBox, 0, 196, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(dayBox, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(monthBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(yearBox, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(omraadeBox, 0, 196, Short.MAX_VALUE)))
                    .addComponent(tilknyt, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(medarbTypeLbl)
                    .addComponent(medarbTypeBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(medarbLbl)
                    .addComponent(medarbBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(datoLbl)
                    .addComponent(monthBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(yearBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dayBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(omraadeLbl)
                    .addComponent(omraadeBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(tilknyt)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void medarbTypeBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_medarbTypeBoxActionPerformed
        // Sletter alt i comboboxen så den er klar til at modtage ny data
        medarbBox.removeAllItems();

        // afhængig af den valgte medarbejder type køres en sql query for at generere en liste af medarbejdere
        switch (medarbTypeBox.getSelectedIndex()) {
            case 0:
                result = dbcon.executeQuery("SELECT fornavn, efternavn FROM personale WHERE type = 0");
                break;
            case 1:
                result = dbcon.executeQuery("SELECT fornavn, efternavn FROM personale WHERE type = 1");
                break;
            case 2:
                result = dbcon.executeQuery("SELECT fornavn, efternavn FROM personale WHERE type = 2");
                break;
        }

        // medarbejder combo boxewn bliver fyldt med de medarbejdere der har den valgte type
        try {
            while (result.next()) {
                medarbBox.addItem(new String(result.getString(1) + " " + result.getString(2)));
            }
        } catch (SQLException e) {

            // Hvis der er en SQL Exception vil der komme en fejl besked i en Message Dialog
            JOptionPane.showMessageDialog(null, "SQL fejl: " + e.getLocalizedMessage());
        }

        // Forbindelsen til databasen lukkes efter brug
        dbcon.close();
    }//GEN-LAST:event_medarbTypeBoxActionPerformed

    private void dayBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dayBoxActionPerformed
        
    }//GEN-LAST:event_dayBoxActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel datoLbl;
    private javax.swing.JComboBox dayBox;
    private javax.swing.JComboBox medarbBox;
    private javax.swing.JLabel medarbLbl;
    private javax.swing.JComboBox medarbTypeBox;
    private javax.swing.JLabel medarbTypeLbl;
    private javax.swing.JComboBox monthBox;
    private javax.swing.JComboBox omraadeBox;
    private javax.swing.JLabel omraadeLbl;
    private javax.swing.JButton tilknyt;
    private javax.swing.JComboBox yearBox;
    // End of variables declaration//GEN-END:variables
}
